package com.btkjgc.utils;

import org.apache.log4j.Logger;

import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.model.JSONValue;

public class LogUtils
{
	static Logger logger = Logger.getLogger(LogUtils.class);

	public static void dump(byte[] data)
	{
		StringBuilder builder = new StringBuilder();
		builder.append(String.format("BYTES[%d]: ", data.length));
		for (int i = 0; i < data.length; i++)
		{
			builder.append(String.format("%02x ", data[i]));
		}
		logger.info(builder.toString());
	}

	public static void info(Object obj)
	{
		logger.info(obj);
	}

	public static void debug(Object obj)
	{
		logger.debug(obj);
	}

	public static void error(Object obj)
	{
		logger.error(obj);
	}

	public static void error(Object obj, Throwable e)
	{
		logger.error(obj, e);
	}

	public static void dump(Object data)
	{
		JSONValue jsonValue;
		try
		{
			jsonValue = JSONMapper.toJSON(data);
			String jsonStr = jsonValue.render(false);
			logger.info(data.getClass().getSimpleName() + jsonStr);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public static void dump(String prefix, Object data)
	{
		JSONValue jsonValue;
		try
		{
			jsonValue = JSONMapper.toJSON(data);
			String jsonStr = jsonValue.render(false);
			logger.info(prefix + ":" + data.getClass().getSimpleName()
					+ jsonStr);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
